package org.duang.pay.request;

import java.util.Map;

import org.duang.common.exceptios.ServiceException;
import org.duang.pay.core.IPayRequest;
import org.duang.pay.core.PayFactory;
import org.duang.pay.utils.PayAppEnum;

import com.google.common.base.Splitter;

/**
 * 支付对象
 * @author laotang
 *
 */
public class PayRequest implements IPayRequest {

	private String appid; // 应用ID
	private String orderid; // 合作商户网站唯一订单号
	private String subject; // 商品名称
	private Map<String,String> body; // 商品详情
	private double price; // 总金额(元为单位)
	private String callbacktype; // 支付方式(第三方支付平台，微信W, 支付宝Z)
	private String paytype; // 商家类型
	private String sign; // 签名字符串
	
	public PayRequest() {
		this.appid = PayFactory.getAppId(); 
	}
	
	public PayRequest(PayAppEnum appIdEnum) {
		this.appid = appIdEnum.name().toString();
	}
	

	public PayRequest(String orderId, String subject, Map<String,String> body, double price, String callbacktype, String paytype,
			String sign) {
		super();
		this.appid = PayFactory.getAppId();
		this.orderid = orderId;
		this.subject = subject;
		this.body = body;
		this.price = price;
		this.callbacktype = callbacktype;
		this.paytype = paytype;
		this.sign = sign;
	}
	
	public PayRequest(PayAppEnum appIdEnum, String orderId, String subject, Map<String,String> body, double price, String callbacktype, String paytype,
			String sign) {
		super();
		this.appid = appIdEnum.name().toString();
		this.orderid = orderId;
		this.subject = subject;
		this.body = body;
		this.price = price;
		this.callbacktype = callbacktype;
		this.paytype = paytype;
		this.sign = sign;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(PayAppEnum appEnum) {
		this.appid = appEnum.name().toString();
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public Map<String,String> getBody() {
		return body;
	}

	public void setBody(Map<String,String> body) {
		this.body = body;
	}
	
	public void setBody(String jsonBody) {
		if(!jsonBody.startsWith("{") && !jsonBody.endsWith("}")) {
			throw new ServiceException("jsonBody不是一个json格式的字符串,必须是{}开头与结尾");
		}
		jsonBody = jsonBody.substring(1, jsonBody.length()-1);
		this.body = Splitter.on(",").omitEmptyStrings().trimResults().withKeyValueSeparator("=").split(jsonBody);
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getCallbacktype() {
		return callbacktype;
	}

	public void setCallbacktype(String callbacktype) {
		this.callbacktype = callbacktype;
	}

	public String getPaytype() {
		return paytype;
	}

	public void setPaytype(String paytype) {
		this.paytype = paytype;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

}
